package com.myacg.editor.common.unit.panel.taskpanel;

import java.awt.BorderLayout;

import javax.swing.JPanel;

import com.myacg.core.task.KeepTask;
import com.myacg.core.task.KeepTaskManager;
import com.myacg.editor.common.entity.CoupleDao;
import com.myacg.editor.common.unit.list.FlowListPanel;
import com.myacg.editor.common.unit.list.ListEvent;
import com.myacg.editor.common.unit.list.item.CoupleDataListItem;
import com.myacg.editor.common.unit.list.item.FlowListItem;
/**
 * 构造可视的任务消息队列
 * @author Administrator
 *
 */
public class KeepTaskManagerPanel extends JPanel{
	FlowListPanel<CoupleDao> taskList;
	KeeptaskInfoPanel  infoPanel ;
	ListEvent<CoupleDao>  listEvent ; //列表事件
	KeepTaskManager  taskManager;
	public KeepTaskManagerPanel(){
		this.listEvent   =  this.getListEvent();
		this.taskList = new FlowListPanel<>(CoupleDataListItem.class,listEvent);
		this.taskManager = KeepTaskManager.getInstance();
		this.setLayout(new BorderLayout());
		this.add(this.taskList,BorderLayout.WEST);
		this.infoPanel = new KeeptaskInfoPanel();
		this.add(this.infoPanel , BorderLayout.CENTER);	
		
		this.addTask(new UIKeepTask(10){

				@Override
				public void execute() {
					if( this.getParent().getTaskList().getSelectId()!=null){
						this.getParent().getInfoPanel().updateData();
					}else{
						
					}
				}
				@Override
				public void overTask() {
				}}, "刷新任务详情面板");
	}
	
	
	private ListEvent getListEvent(){
		return new ListEvent<CoupleDao>() {
			@Override
			public void selectChangeEvent(FlowListPanel<CoupleDao> listPanel, FlowListItem<CoupleDao> listItem) {
				if(listItem.isSelected()){
					UIKeepTask task=(UIKeepTask)taskManager.getTask(listItem.getId());
					 infoPanel.setUiKeepTask(task);
				}
			}
			@Override
			public void hoverChangeEvent(FlowListPanel<CoupleDao> listPanel, FlowListItem<CoupleDao> listItem) {
				// TODO Auto-generated method stub
			}
		};
	}
	public void addTask(UIKeepTask task,String taskName){
		task.setParent(this);
		task.setTaskName(taskName);
		this.taskManager.addTask(task);
		this.taskList.addItem(task.getId(),new CoupleDao(taskName+" : ",task.getExecuteCount()+"次"));  
	}
	public void removeTask(String id){
		KeepTask task = this.taskManager.getTask(id);
		if(task!=null){
			task.over(); //终止任务
		}
	}


	public FlowListPanel<CoupleDao> getTaskList() {
		return taskList;
	}


	public void setTaskList(FlowListPanel<CoupleDao> taskList) {
		this.taskList = taskList;
	}


	public KeepTaskManager getTaskManager() {
		return taskManager;
	}


	public KeeptaskInfoPanel getInfoPanel() {
		return infoPanel;
	}


	public void setInfoPanel(KeeptaskInfoPanel infoPanel) {
		this.infoPanel = infoPanel;
	}
 
}
